import '../../public.dart';
import 'package:flutter_slidable/flutter_slidable.dart';

class SlidableBar extends StatefulWidget {
  final Widget child;
  final Function()? onDelete;
  const SlidableBar({super.key, required this.child, this.onDelete});

  @override
  State<SlidableBar> createState() => _SlidablebarState();
}

class _SlidablebarState extends State<SlidableBar>
    with SingleTickerProviderStateMixin {
  late final controller = SlidableController(this);

  @override
  Widget build(BuildContext context) {
    return Slidable(
      controller: controller,
      key: widget.key,
      endActionPane: widget.onDelete != null
          ? ActionPane(
              motion: const ScrollMotion(),
              children: [
                SlidableAction(
                  // An action can be bigger than the others.
                  flex: 2,
                  onPressed: (_) {
                    widget.onDelete!();
                    controller.close();
                  },
                  backgroundColor: Color(0xFFFE4A49),
                  foregroundColor: Colors.white,
                  icon: Icons.delete,
                  label: 'Delete',
                ),
              ],
            )
          : null,
      child: widget.child,
    );
  }

  @override
  void dispose() {
    // Dispose the SlidableController before calling super.dispose()
    controller.dispose();
    super.dispose();
  }
}
